package com.qfedu.scode.util;

import com.qfedu.scode.config.SystemConfig;
import io.jsonwebtoken.JwtBuilder;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;

import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.util.Date;

/**
 * @program: StartCoding
 * @description:
 * @author: Feri(邢朋辉)
 * @create: 2021-01-08 10:56
 */
public class JwtUtil {
    /**
     * 生成令牌
     * @param msg 要生成令牌的内容*/
    public static String createToken(String msg){
        //实例化建造器
        JwtBuilder builder= Jwts.builder();
        builder.setSubject(msg);
        builder.setIssuedAt(new Date());
        builder.setExpiration(DateUtil.addMinu(SystemConfig.JWT_TOKENTIME));
        //设置加密算法
        builder.signWith(SignatureAlgorithm.HS256,createKey());

        return builder.compact();
    }
    //
    private static SecretKey createKey(){
        byte[] data=SystemConfig.JWT_KEY.getBytes();
        SecretKey key=new SecretKeySpec(data,"AES");
        return key;
    }
}
